package com.ittx.dzt.meituan.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ittx.dzt.edi.constant.PlatformMessageConstant;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.vo.ResponseMessageVO;
import com.ittx.dzt.log.monitor.ExceptionLog;
import com.ittx.dzt.meituan.constant.MeituanApiConstant;
import com.ittx.dzt.meituan.handler.AbstractBaseMeituanApiHandler;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import open.dto.PackageDS;
import open.dto.PackageDetailDS;
import open.dto.ShipmentDispatchDto;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发货
 * <p>
 * cy.shipment.dispatch
 * /api/v1/gw/order/perform(发货，支持拆单，每次只能传一个包裹) cy.shipment.dispatch https://tscc.meituan.com/home/doc/bulkPurchasing/504
 * </p>
 *
 * @author gywang
 * @since 2024-08-01
 */
@Service
@Slf4j
public class MeituanShipmentDispatchService extends AbstractBaseMeituanApiHandler<ShipmentDispatchDto> {

    @Override
    public String getMeituanMethod() {
        return MeituanApiConstant.ORDER_SHIP;
    }

    @Override
    public String getHttpMethod() {
        return MeituanApiConstant.ApiHttpMethod.POST;
    }

    @Override
    public String getLocalApiMethod() {
        return PlatformApiConstant.InboundInterface.SHIPMENT_DISPATCH;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, ShipmentDispatchDto shipmentDispatchDTO) {
        try {

            Map<String, String> params = new HashMap<>();
            params.put("poi_id", getPoIId(platformApiParamDTO));
            params.put("order_view_id", shipmentDispatchDTO.getSourceOrderCode());
            //是否拆单 0不拆单，1拆单
            if (shipmentDispatchDTO.getPacks() != null && shipmentDispatchDTO.getPacks().size() > 1)
                params.put("is_split", "1");
            //物流信息list，目前list中只支持传递一个

            if (CollUtil.isEmpty(shipmentDispatchDTO.getPacks())) {
                List<Map<String, Object>> list = new ArrayList<>();
                Map<String, Object> express = new HashMap<>();
                express.put("expressCode", shipmentDispatchDTO.getCarrierCode());
                express.put("expressNo", shipmentDispatchDTO.getWaybillCode());
                list.add(express);
                params.put("express_info_list", JacksonTool.toJson(list));
                return this.executeMeituanApi(platformApiParamDTO.getStoreConfig(), params, platformApiParamDTO.getRequestId());
            }
            //多包裹
            for (PackageDS pack : shipmentDispatchDTO.getPacks()) {
                List<Map<String, Object>> list = new ArrayList<>();
                Map<String, Object> express = new HashMap<>();
                express.put("expressCode", pack.getCarrierCode());
                express.put("expressNo", pack.getWaybillCode());
                express.put("package_id", pack.getPackageNo());
                List<Map<String, Object>> details = new ArrayList<>();
                for (PackageDetailDS d : pack.getDetails()) {
                    Map<String, Object> md = new HashMap<>();
                    md.put("spu_id", d.getSourceSpuId());
                    md.put("sku_id", d.getSourceSkuId());
                    details.add(md);
                }
                express.put("product_list", details);
                list.add(express);
                params.put("express_info_list", JacksonTool.toJson(list));
                Result result = this.executeMeituanApi(platformApiParamDTO.getStoreConfig(), params, platformApiParamDTO.getRequestId());
                if (!result.isSuccess()) {
                    return result;
                }
            }
            return Result.success();
        } catch (Exception e) {
            String msg = StrUtil.format(PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), this.getMeituanMethod(), e.getMessage());
            ExceptionLog.error(e, PlatformMessageConstant.MSG_PLAT_0003_MSG, platformApiParamDTO.getRequestId(), this.getPlatformCode(), this.getMeituanMethod(), e.getMessage());
            return Result.error(new ResponseMessageVO(PlatformMessageConstant.MSG_PLAT_0003, msg)).addMsg(msg);
        }
    }
}
